package gson.emlog.adapter;


import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import gson.emlog.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentsAdapter extends BaseAdapter {
	private JSONArray items;
	private Context mContext;

	public CommentsAdapter(JSONArray data, Context mContext) {
		super();
		this.items = data;
		this.mContext = mContext;
	}

	public int getCount() {
		return items.size();
	}

	public JSONObject getItem(int position) {
		try {
			return (JSONObject) items.get(position);
		} catch (Exception e) {
			return null;
		}
	}

	public long getItemId(int position) {
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return Integer.parseInt(obj.get("id").toString());
			}
		} catch (Exception e) {
			return position;
		}
		return position;
	}
	
	public long getBlogId(int position) {
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return Integer.parseInt(obj.get("gid").toString());
			}
		} catch (Exception e) {
			return position;
		}
		return position;
	}
	
	public String getHide(int position) {
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return obj.get("hide").toString();
			}
		} catch (Exception e) {
			return "n";
		}
		return "n";
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// 获取layout实例对象
		LayoutInflater lif = LayoutInflater.from(mContext);
		LinearLayout item = (LinearLayout) lif
				.inflate(R.layout.commentitem, null);
		if (item != null) {
			try {
				JSONObject data = (JSONObject) items.get(position);
				// 赋值
				TextView pst = (TextView) item.findViewById(R.id.cmpeplo);
				pst.setText(data.get("pst").toString()+(data.get("hide").equals("y") ? "[待审]":""));
				
				// 赋值
				TextView date = (TextView) item.findViewById(R.id.cmdate);
				date.setText(data.get("d").toString());
				
				// 赋值
				TextView cmct = (TextView) item.findViewById(R.id.cmct);
				cmct.setText(data.get("cmt").toString());
				
				// 赋值
				TextView cmlog = (TextView) item.findViewById(R.id.cmlog);
				cmlog.setText(data.get("bt").toString());
				return item;
			} catch (Exception e) {
				return convertView;
			}
		}

		return convertView;
	}
}
